<?php

namespace App\Http\Controllers\Api\V1\Menu;

use App\Http\Validate\Api\Menu\MenuRuleV;
use App\Lib\Util\ErrorCode;
use App\Services\Menu\MenuService;
use Illuminate\Http\Request;

class MenuController
{

    private $MenuService;

    public function __construct(MenuService $MenuService)
    {
        $this->MenuService = $MenuService;
    }

  /**
   * 获取权限列表
   * @param Request $request
   * @return mixed
   * @throws \Exception
   */
    public function getMenuList(Request $request)
    {
      $param = $request->all();
      $validator = MenuRuleV::get($param);
      if ($validator->fails()) {
        throw new \Exception($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
      }
      $arrMenuList = $this->MenuService->getMenuList();
      return response()->success($arrMenuList);
    }

  /**
   * 获取权限列表（简版）
   * @param Request $request
   * @return mixed
   * @throws \Exception
   */
    public function getMenuListOnly(Request $request)
    {
      $param = $request->all();
      $validator = MenuRuleV::get($param);
      if ($validator->fails()) {
        throw new \Exception($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
      }
      $arrMenuList = $this->MenuService->getMenuListOnly();
      return response()->success($arrMenuList);
    }

    /**
     * 增加权限
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function addMenu(Request $request)
    {
      $param = $request->all();
      $validator = MenuRuleV::add($param);
      if ($validator->fails()) {
        throw new \Exception($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
      }
      $arrMenuList = $this->MenuService->addMenu($param);
      return response()->success($arrMenuList);
    }

  /**
   * 修改权限
   * @param Request $request
   * @return mixed
   * @throws \Exception
   */
    public function editMenu(Request $request)
    {
      $param = $request->all();
      $validator = MenuRuleV::edit($param);
      if ($validator->fails()) {
        throw new \Exception($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
      }
      $arrMenuList = $this->MenuService->editMenu($param);
      return response()->success($arrMenuList);
    }
    /**
     * 删除权限
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function delMenu(Request $request)
    {
      $param = $request->all();
      $validator = MenuRuleV::del($param);
      if ($validator->fails()) {
        throw new \Exception($validator->errors()->first(),ErrorCode::PARAMS_ERROR);
      }
      $arrMenuList = $this->MenuService->delMenu($param['key']);
      return response()->success($arrMenuList);
    }
}
